package com.xlzcx.xltz.service.a_relation;

import com.xlzcx.xltz.dao.ARelationMapper;
import com.xlzcx.xltz.entity.ARelation;
import com.xlzcx.xltz.utils.SnowFlakeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * 作者: 李咨辰
 * 文件名: ARelationServiceImpl.java
 * 路径: com.xlzcx.xltz.service.relation
 * 时间: 2024-10-23 12:11
 * 版本: 1.0
 * 描述: TODO 用户关系
 **/
@Service
public class ARelationServiceImpl implements ARelationService {
    /**
     * 调用dao层
     */
    @Resource
    private ARelationMapper aRelationMapper;


    /**
     * 操作用户关系
     *
     * @param relation 关系内容 userId当前账户 , targetUserId 对方账账户 , type 0 操作关注 type 2 操作拉黑
     * @return 成返回true 失败返回false
     */
    @Override
    public boolean controlsRelation(HashMap<String, Long> relation) {
        Long userId = relation.get("userId");
        Long targetUserId = relation.get("targetUserId");
        Integer type = relation.get("type") != null ? relation.get("type").intValue() : null;
        ARelation aRelation = aRelationMapper.getRelationByUserIdAndTargetUserId(userId, targetUserId);
        int i;
        if (aRelation != null) {
            aRelation.setUpdateTime(new Date());
            if (type != null && type == 0) {
                aRelation.setFollow(aRelation.getFollow() == 0 ? 1 : 0);
            } else {
                aRelation.setShield(aRelation.getShield() == 0 ? 1 : 0);
            }
            i = aRelationMapper.updataRelation(aRelation);
        } else {
            aRelation = new ARelation();
            aRelation.setId(SnowFlakeUtil.getFlowIdInstance().nextId());
            aRelation.setUserId(userId);
            aRelation.setTargetUserId(targetUserId);
            aRelation.setCreateTime(new Date());
            if (type != null && type == 0) {
                aRelation.setFollow(1);
            } else {
                aRelation.setShield(1);
            }
            i = aRelationMapper.addRelation(aRelation);
        }

        return i > 0;
    }
}
